package com.heima.search.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.common.constant.GlobalConstant;
import com.heima.model.search.vos.SearchArticleVo;
import com.heima.search.service.ESService;
import jdk.nashorn.internal.objects.Global;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class ESServiceImpl implements ESService {
    @Autowired
    RestHighLevelClient client;
    @Override
    public void saveSearchArticleVo(SearchArticleVo searchArticleVo) {
        IndexRequest indexRequest=new IndexRequest(GlobalConstant.APP_INFO_ARTICLE_TAG);
        indexRequest.id(searchArticleVo.getId().toString());
        indexRequest.source(JSON.toJSONString(searchArticleVo), XContentType.JSON);
        try {
            client.index(indexRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
